// pages/mine/test/test.js
const app = getApp()
import utils from '../../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formId: null,
    webShow: false,
    code: null,

    popupVisible: false,
    currentDate: new Date().getTime(),
    dateShow: utils.formatTime(new Date().getTime(), 'Y年M月'),
    minDate: 1521478523211,
    maxDate: 1621478523211,
    lastDate: [],
  },
  showPopup() {
    this.setData({
      popupVisible: true,
    })
  },
  onPopupClose() {
    
  },
  /**
   * 时间选择 滑动变换 动态赋值 (lastDate未变化)
   */
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    })
  },
  /**
   * 时间选择 点击确认 赋值(lastDate修改)
   */
  dateConfirm(event) {
    this.setData({
      popupVisible: false,
      currentDate: event.detail,
      dateShow: utils.formatTime(event.detail, 'Y年M月'),
      lastDate: event.detail,
    })
  },
  /**
   * 时间选择 点击取消 将lastDate设为初始
   */
  dateCancel() {
    this.setData({
      currentDate: this.data.lastDate,
      popupVisible: false,
    })
  },
  formSubmit (e) {
    console.log('form发生了submit事件，formId为：', e.detail)
    var that = this;
    this.setData({
      formId: e.detail.formId
    })
    app.globalData.request('post', 'manage/unit_building/cs', {
      formId: e.detail.formId
    }).then(res => {
      console.log(res)
    }).catch(err => {

    })
  },
  getURL(res) {
    console.log(res)
  },
  /**
   * 
   */
  webReady(e) {
    let  self = this

    function getParam(src, paramName) {
      let result = ''
      let arrays = src.split('?')[1].split('&')
      arrays.map(item => {
        if (item.indexOf([paramName]) == 0) {
          result = item
        }
      })
      return result.slice(paramName.length + 1)
    }

    setTimeout(_ => {
      self.setData({
        webShow: false,
        code: getParam(e.detail.src, 'code')
      })
      console.log(getParam(e.detail.src, 'code'))
    }, 1500)
    
    
    
    // app.globalData.request('get', 'manage/unit_building/cs', {
    //   code: getParam(e.detail.src, 'code')
    // }).then(res => {
    //   console.log(res)
    // })
  },
  /**
   * 调起微信支付
   */
  wechatPay() {
    app.globalData.request('post', 'manage/unit_building/pay').then(res => {
      wx.requestPayment({
        timeStamp: res.msg.timeStamp,
        nonceStr: res.msg.nonceStr,
        package: res.msg.package,
        signType: res.msg.signType,
        paySign: res.msg.paySign,
        success(res) {
          wx.showModal({
            title: '支付成功!',
          })
        },
        fail(err) {
          wx.showModal({
            title: '支付失败!',
            content: err
          })
        }
      })
    }).catch(err => {
      console.log(err)
    })
  }
})